package controller

import (
	"SystemExploration/ability"
	"SystemExploration/mongodb"
	"SystemExploration/task"
	"SystemExploration/task/itemdb"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
)

type Tasks struct{}

// 路由
func (that *Tasks) Router(engine *gin.RouterGroup) {
	engine.POST("/task/list", that.lists)
	engine.POST("/task/add", that.add)
	engine.POST("/task/up", that.up)
	engine.POST("/task/state", that.state)
}

//任务列表
func (that *Tasks) lists(c *gin.Context) {
	listRespone := TaskListRequest{}
	if err := c.ShouldBind(&listRespone); err != nil {
		c.JSON(200, gin.H{
			"code": "402",
			"msg":  "参数错误",
		})
		return
	}
	list, count, _ := mongodb.FindTaskAll(listRespone.Page, listRespone.Limit)
	var resplist = []TaskListData{}
	for _, v := range list {
		var elem TaskListData
		elem.Name = v.Name
		elem.Machine = v.Machine
		elem.Crons = v.Crons
		elem.People = v.People
		elem.State = v.State
		elem.Url = v.Url
		resplist = append(resplist, elem)
	}

	response := &TaskListRespone{
		Code:  200,
		Msg:   "成功",
		Count: count,
		Data:  resplist,
	}

	c.JSON(200, response)
	return

}

func (that *Tasks) add(c *gin.Context) {
	addRespone := TaskAddRequest{}
	if err := c.ShouldBind(&addRespone); err != nil {
		c.JSON(200, gin.H{
			"code": "402",
			"msg":  "参数错误",
		})
		return
	}

	//先看看有么有这个任务
	//查数据库有没有
	info := new(mongodb.Taskcron)
	filter := bson.D{{"name", addRespone.Name}}
	err := mongodb.FindTaskOne(filter, info)
	if err == nil {
		c.JSON(200, gin.H{
			"code": "403",
			"msg":  "该任务已存在",
		})
		return
	}

	//插入数据
	taskinfo := &mongodb.Taskcron{
		Name:    addRespone.Name,
		Machine: addRespone.Machine,
		Crons:   addRespone.Crons,
		People:  addRespone.People,
		State:   0,
		Type:    1,
		Url:     addRespone.Url,
	}
	err = mongodb.FindTaskAdd(taskinfo)
	if err != nil {
		ability.DingErr(taskinfo.Machine, "mongodb.FindTaskAdd"+err.Error())
		c.JSON(200, gin.H{
			"code": "404",
			"msg":  "添加任务失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": "200",
		"msg":  "录入任务成功",
	})
}

func (that *Tasks) up(c *gin.Context) {
	upRespone := TaskUpRequest{}
	if err := c.ShouldBind(&upRespone); err != nil {
		c.JSON(200, gin.H{
			"code": "402",
			"msg":  "参数错误",
		})
		return
	}

	//先看看有么有这个任务
	//查数据库有没有
	info := new(mongodb.Taskcron)
	filter := bson.D{{"name", upRespone.Name}}
	if err := mongodb.FindTaskOne(filter, info); err != nil {
		c.JSON(200, gin.H{
			"code": "403",
			"msg":  "该任务不存在",
		})
		return
	}

	//操作定时任务
	taskContent := itemdb.ItemInfo{
		Name:    upRespone.Name,
		Machine: upRespone.Machine,
		Crons:   upRespone.Crons,
		Url:     upRespone.Url,
		People:  upRespone.People,
		State:   upRespone.State,
		Type:    1,
	}
	task.AddTask(taskContent)

	filters := bson.D{{"name", upRespone.Name}}

	//更新内容
	update := bson.M{
		"$set": bson.M{
			"name":    upRespone.Name,
			"machine": upRespone.Machine,
			"crons":   upRespone.Crons,
			"people":  upRespone.People,
			"state":   upRespone.State,
			"url":     upRespone.Url,
		},
	}

	if err := mongodb.FindTaskUp(filters, update); err != nil {
		c.JSON(200, gin.H{
			"code": "404",
			"msg":  "更新任务失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": "200",
		"msg":  "更新任务成功",
	})

}

func (that *Tasks) state(c *gin.Context) {
	stateRespone := TaskStateRequest{}
	if err := c.ShouldBind(&stateRespone); err != nil {
		c.JSON(200, gin.H{
			"code": "402",
			"msg":  "参数错误",
		})
		return
	}

	//先看看有么有这个任务
	//查数据库有没有
	info := new(mongodb.Taskcron)
	filter := bson.D{{"name", stateRespone.Name}}
	if err := mongodb.FindTaskOne(filter, info); err != nil {
		c.JSON(200, gin.H{
			"code": "403",
			"msg":  "该任务不存在",
		})
		return
	}

	//操作定时任务
	taskContent := itemdb.ItemInfo{
		Name:    info.Name,
		Machine: info.Machine,
		Crons:   info.Crons,
		Url:     info.Url,
		People:  info.People,
		State:   stateRespone.State,
		Type:    1,
	}
	task.AddTask(taskContent)

	filters := bson.D{{"name", stateRespone.Name}}

	//更新内容
	update := bson.M{
		"$set": bson.M{
			"state": stateRespone.State,
		},
	}

	if err := mongodb.FindTaskUp(filters, update); err != nil {
		c.JSON(200, gin.H{
			"code": "404",
			"msg":  "更新任务失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": "200",
		"msg":  "更新任务成功",
	})

}

func (that *Tasks) del(c *gin.Context) {

}
